package com.action;
import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.beans.AdminInfo;
import com.dao.AdminDao;

@Controller @RequestMapping("/admin")
public class AdminAction {
	
	@Resource
	private AdminDao  adminDao;
	
	@RequestMapping("/login")
	public String login(String adminName,String password,HttpSession session,ModelMap m) {
		AdminInfo admin=adminDao.login(adminName,password);
		if(admin!=null) {
			session.setAttribute("g_admin", admin);	
	      //由于静态html并不支持响应头带有 post 的应答包,所以不能直接写成:return index.html
			return "redirect:/index.html";
		}
		else {
			m.put("msg", "用户名或密码错误,登录失败"); 
			return "/login.jsp";
		}
	}
	
	@RequestMapping("/logout")
	public void logout(HttpSession session,HttpServletResponse response) throws IOException {
		session.removeAttribute("g_admin");	
		session.removeAttribute("studentList");
		response.getWriter().print("<script>window.top.location.href='/login.jsp'</script>");
	}
	
	//刷session 防止过期
	@GetMapping("/refresh")
	public void refreshSession(HttpSession session){
		AdminInfo admin =(AdminInfo)session.getAttribute("g_admin");	
		session.setAttribute("g_admin",admin);
	}
}
